{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63bded09",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from dotenv import load_dotenv\n",
    "from openai import OpenAI\n",
    "from IPython.display import Markdown, display"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad7fd2a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# load env\n",
    "load_dotenv(override=True)\n",
    "openai_api_key = os.getenv('OPENAI_API_KEY')\n",
    "\n",
    "if openai_api_key:\n",
    "    print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
    "else:\n",
    "    print(\"OpenAI API Key not set\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94409e7a",
   "metadata": {},
   "outputs": [],
   "source": [
    "openAI = OpenAI()\n",
    "ollama_url = \"http://localhost:11434/v1\"\n",
    "\n",
    "ollama = OpenAI(base_url=ollama_url, api_key=\"ollama_key\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f6fda513",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "gpt_5_1 = \"gpt-5.1\"\n",
    "gpt_5_mini=\"gpt-5-mini\"\n",
    "rajesh_convs=[\"\"\"Hi I am a stock market expert and would love to help in conversations and discussions\"\"\"]\n",
    "subbarao_convs=[\"I want to do a detailed analysis on the shakti Pumps india\"]\n",
    "padma_convs=[\"ya would love to know more on shakti pumps as subbarao mentioned\"]\n",
    "\n",
    "\n",
    "def stockMarketExpert():\n",
    "    conv_history=\"\"\n",
    "    messages = [\n",
    "    {\"role\": \"system\", \"content\": \"\"\"You are an expert analyst specializing in the Indian stock market named Rajesh, known for rigorous, data-driven, and deeply researched evaluations. Your objective is to identify fundamentally strong companies with long-term multibagger potential—especially those currently trading below their intrinsic value due to temporary market neglect.\n",
    "    When analyzing a company, you thoroughly examine valuation metrics (including P/E ratio and historical valuation ranges), financial strength, competitive advantages, management quality, sector dynamics, growth prospects, and all other relevant qualitative and quantitative parameters needed to judge long-term investment potential. You provide clear, structured, unbiased, and long-term–focused insights.\n",
    "    You will participate in a conversation where two or more users may ask questions or discuss ideas with you simultaneously. You must respond to each user clearly, address their points individually when needed, and help guide the discussion with accurate, well-reasoned analysis.Remember your response should not go beyond 100 charecters which means you need to crisply answer whether u think it is good to buy a stock or no and reasons\"\"\"}\n",
    "    ]\n",
    "\n",
    "    for rajesh_conv,subbarao_conv,padma_conv in zip(rajesh_convs, subbarao_convs, padma_convs):\n",
    "        # Format conversation in markdown\n",
    "        conv_entry = f\"Rajesh: {rajesh_conv} \\n\\n subbarao: {subbarao_conv} \\n\\n padma: {padma_conv} \\n\\n\"\n",
    "        # Append to conv_history\n",
    "        conv_history += conv_entry\n",
    "    messages.append({\"role\": \"user\", \"content\": f\"\"\"Hi i am a stock market expert who helps in analysis and give u buy and sell calls based on prices and the best entry timing based on conversation \\n\\n {conv_history}\"\"\"})\n",
    "    response = openAI.chat.completions.create(model=gpt_5_1, messages=messages)\n",
    "    rajesh_convs.append(response.choices[0].message.content)\n",
    "    return response.choices[0].message.content\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83c19ce9",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def call_stockAnalyzer1():\n",
    "    conv_history=\"\"\n",
    "    user_prompt_subbarao=f\"\"\"you being Subba rao post your answer or query based on the conversation that is being happening \\n\\n {conv_history}\"\"\"\n",
    "    stockAnalyzer1=[\n",
    "        { \"role\": \"system\", \"content\":\"Here you are a stock market analyser named Subbarao and in converation with Rajesh and Padma, and you would ask the question to the stock market expert on which is the best stock to invest in giving a specific stock name and get the response back from stock market expert.If you have any question related to why do the expert think the stock is good or bad you would raise more question to the stock market expert on this. Remember your question/answer should not exceed more than 100 words. Have it to eb as crisp as possible\" },\n",
    "        ]\n",
    "    for rajesh_conv,subbarao_conv,padma_conv in zip(rajesh_convs, subbarao_convs, padma_convs):\n",
    "        # Format conversation in markdown\n",
    "        conv_entry = f\"Rajesh: {rajesh_conv} \\n\\n subbarao: {subbarao_conv} \\n\\n padma: {padma_conv} \\n\\n\"\n",
    "        # Append to conv_history\n",
    "        conv_history += conv_entry\n",
    "    stockAnalyzer1.append({'role': \"user\", \"content\": user_prompt_subbarao})\n",
    "    # display(Markdown(conv_history))\n",
    "    \n",
    "    response=openAI.chat.completions.create(model=gpt_5_mini, messages=stockAnalyzer1)\n",
    "    subbarao_convs.append(response.choices[0].message.content)\n",
    "    # display(Markdown(response.choices[0].message.content))\n",
    "    return response.choices[0].message.content\n",
    "\n",
    "        \n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "adeb7d95",
   "metadata": {},
   "outputs": [],
   "source": [
    "def call_stockAnalyzer2():\n",
    "    conv_history=\"\"\n",
    "    user_prompt_padma=f\"\"\"you being Padma post your answer or query based on the conversation that is being happening \\n\\n {conv_history}\"\"\"\n",
    "    stockAnalyzer2=[\n",
    "        { \"role\": \"system\", \"content\":\"Here you are a stock market analyser named Padma and in converation with Rajesh and Subbarao, and you would ask the question to the stock market expert on which is the best stock to invest in giving a specific stock name and get the response back from stock market expert.If you have any question related to why do the expert think the stock is good or bad you would raise more question to the stock market expert on this. Remember your question/answer should not exceed more than 100 words. Have it to eb as crisp as possible\" },\n",
    "        ]\n",
    "    for rajesh_conv,subbarao_conv,padma_conv in zip(rajesh_convs, subbarao_convs, padma_convs):\n",
    "        # loop through the conversation in as rajesh_conv: {message sent by expert}, subbarao_conv: {message sent by subbarao_conv}, padma_conv:{message sent by padma_conv} in a markdown file\n",
    "        # Format conversation in markdown\n",
    "        conv_entry = f\"Rajesh: {rajesh_conv} \\n\\n subbarao: {subbarao_conv} \\n\\n padma: {padma_conv} \\n\\n\"\n",
    "        # Append to conv_history\n",
    "        conv_history += conv_entry\n",
    "    stockAnalyzer2.append({'role': \"user\", \"content\": user_prompt_padma})\n",
    "    # display(Markdown(conv_history))\n",
    "    \n",
    "    response=ollama.chat.completions.create(model=\"llama3.2\", messages=stockAnalyzer2)\n",
    "    padma_convs.append(response.choices[0].message.content)\n",
    "    # display(Markdown(response.choices[0].message.content))\n",
    "    return response.choices[0].message.content\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f3738c02",
   "metadata": {},
   "outputs": [],
   "source": [
    "display(Markdown(f\"### Rajesh(StockMarket experts):\\n{rajesh_convs[0]}\\n\"))\n",
    "display(Markdown(f\"### Subbarao:\\n{subbarao_convs[0]}\\n\"))\n",
    "display(Markdown(f\"### Padma:\\n{padma_convs[0]}\\n\"))\n",
    "\n",
    "for i in range(10):\n",
    "    rajesh_expert = stockMarketExpert()\n",
    "    display(Markdown(f\"### rajesh_expert:\\n{rajesh_expert}\\n\"))\n",
    "    \n",
    "    subbarao_stockAnalyser = call_stockAnalyzer1()\n",
    "    display(Markdown(f\"### subbarao_stockAnalyser:\\n{subbarao_stockAnalyser}\\n\"))\n",
    "\n",
    "    padma_stockAnalyser = call_stockAnalyzer2()\n",
    "    display(Markdown(f\"### padma_stockAnalyser:\\n{padma_stockAnalyser}\\n\"))"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
